/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufjf.pos.model.dao;

import br.ufjf.pos.model.Cliente;
import br.ufjf.pos.model.Usuario;
import br.ufjf.pos.util.GenericDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author ice
 */
public class ClienteDAO extends GenericDao<Cliente, Integer> {

    @Override
    public void inserir(Cliente obj) throws ClassNotFoundException, SQLException {

        String sql = "insert into clientes (nome) values (?)";

        ArrayList<String> lista = new ArrayList<String>();

        lista.add(obj.getNome());

        executaDML(sql, lista);

    }

    @Override
    public void alterar(Cliente obj) throws ClassNotFoundException, SQLException {

        String sql = "update clientes set nome = ? where id = ?";

        ArrayList<String> lista = new ArrayList<String>();

        lista.add(obj.getNome());
        lista.add(Integer.toString(obj.getId()));

        executaDML(sql, lista);
    }

    @Override
    public void apagar(Cliente obj) throws ClassNotFoundException, SQLException {

        String sql = "delete from clientes where id = ?";

        ArrayList<String> lista = new ArrayList<String>();

        lista.add(obj.getId() + "");

        executaDML(sql, lista);
    }

    @Override
    public Cliente pesquisarChave(Integer obj) throws ClassNotFoundException, SQLException {

        String sql = "select * from clientes where id = ?";

        ArrayList<String> lista = new ArrayList<String>();

        lista.add(obj.toString());

        ResultSet rs = executaSql(sql, lista);

        if (rs.next()) {

            Cliente c = new Cliente(rs.getInt("id"), rs.getString("nome"));

            return c;

        } else {
            return null;
        }
    }
    
    public ArrayList<Cliente> pesquisarNome(String nome) throws ClassNotFoundException, SQLException {

        String sql = "select * from clientes where nome like ?";

        ArrayList<String> lista = new ArrayList<String>();

        lista.add("%"+ nome + "%");
        
        ResultSet rs = executaSql(sql, lista);

        ArrayList<Cliente> listaClientes = new ArrayList<Cliente>();

        while (rs.next()) {

            Cliente c = new Cliente(rs.getInt("id"), rs.getString("nome"));

            listaClientes.add(c);
        }

        return listaClientes;
    }    

    @Override
    public ArrayList<Cliente> buscarTodos() throws ClassNotFoundException, SQLException {

        String sql = "select * from clientes";

        ArrayList<String> lista = new ArrayList<String>();

        ResultSet rs = executaSql(sql, lista);

        ArrayList<Cliente> listaClientes = new ArrayList<Cliente>();

        while (rs.next()) {

            Cliente c = new Cliente(rs.getInt("id"), rs.getString("nome"));

            listaClientes.add(c);
        }

        return listaClientes;
    }
}
